// pages/my/my.js
const api = require('../../util')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        statusBarHeight: app.globalData.statusBarHeight,

        obj: {},
        is_bind_phone: ''
    },
    // 获取个人信息
    PersonalCenter() {
        var that = this
        api.http('/user/PersonalCenter', 'POST', false, {
        }, function (res) {
            if(res.code == 200) {
                that.setData({
                    obj: res.data
                })
            }
        })
    },
    // 个人信息
    toinformation() {
        if(!api.disable()) {
            return
        }
        wx.navigateTo({
            url: '/pages/information/information',
        })
    },
    // 我的收藏
    tocollect(){
        if(!api.disable()) {
            return
        }
        wx.navigateTo({
          url: '/pages/collect/collect',
        })
    },
    // 健康积分
    tohealthpoints() {
        if(!api.disable()) {
            return
        }
        wx.navigateTo({
            url: '/pages/healthpoints/healthpoints?integral='+this.data.obj.integral,
        })
    },
    // 用药管理
    tomedicationmanage() {
        if(!api.disable()) {
            return
        }
        wx.navigateTo({
            url: '/pages/medicationmanage/medicationmanage',
        })
    },
    // 用药打卡
    todaka() {
        if(!api.disable()) {
            return
        }
        var that = this
        // 判断是否设置用药提醒
        api.http('/MedicationReminder/normalQuantity', 'POST', false, {
        }, function (res) {
            if(res.data.count !== 0) {
                wx.switchTab({
                    url: '/pages/clockin/clockin',
                })
            } else {
                wx.showModal({
                title: '提示',
                content: '未添加用药提醒，是否前往添加？',
                success: function (sm) {
                    if (sm.confirm) {
                        wx.navigateTo({
                            url: '/pages/remind/remind',
                        })
                    } else if (sm.cancel) {
                    }
                }
            })
            }
        })
    },
    // 健康报告
    tohealthreport() {
        if(!api.disable()) {
            return
        }
        wx.navigateTo({
            url: '/pages/healthreport/healthreport',
        })
    },
    // 复诊
    tofollowup() {
        if(!api.disable()) {
            return
        }
        wx.navigateTo({
          url: '/pages/followup/followup',
        })
    },
    // 绑定
    tobinding() {
        if(!api.disable()) {
            return
        }
        wx.navigateTo({
            url: '/pages/binding/binding',
        })  
    },
    // 常见问题
    toproblem() {
        if(!api.disable()) {
            return
        }
        wx.navigateTo({
            url: '/pages/problem/problem',
        })
    },
    // 服务协议
    toagreements() {
        if(!api.disable()) {
            return
        }
        wx.navigateTo({
            url: '/pages/agreements/agreements',
        })
    },
    // 意见反馈
    tofeedback() {
        if(!api.disable()) {
            return
        }
        wx.navigateTo({
            url: '/pages/feedback/feedback',
        })
    },
    getPhoneNumber (e) {
        var that = this
        if (e.detail.errMsg != 'getPhoneNumber:fail no permission'&&e.detail.errMsg != 'getPhoneNumber:fail user deny') {
            console.log(e)
            api.http('/login/bindPhone', 'POST', false, {
                code: e.detail.code
            }, function (res) {
              if(res.code == 200) {
                 that.setData({
                    is_bind_phone: 1
                })
                wx.setStorageSync(app.globalData.miniProgramtype+'is_bind_phone',1)
                wx.showToast({
                    title: res.msg,
                    icon: 'success',
                    duration: 1000
                })
              } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 1000
                })
              }
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                select: 3
            })
        }
        if(!api.disable()) {
            this.setData({
                obj: {}
            })
            return
        }
        var that = this
        if (wx.getStorageSync(app.globalData.miniProgramtype+'access_token')=='') {
            app.wxlogin().then(()=>{
                this.setData({
                    is_bind_phone: wx.getStorageSync(app.globalData.miniProgramtype+'is_bind_phone')
                })
                that.PersonalCenter()
            })
        } else {
            this.setData({
                is_bind_phone: wx.getStorageSync(app.globalData.miniProgramtype+'is_bind_phone')
            })
            that.PersonalCenter()
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    // 分享到朋友圈
    onShareTimeline(){
        return {
        //   title: '',
        // query: 'id=' + this.id,
        // imageUrl: ''
        }
    }
})